﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

#endregion

#region COPYRIGHT

/*
    Copyright (c) 2007, 2008, 2009, 2010 
        Roland Rosenkranz (Glatzemann@email.de)
*/

#endregion

#region LICENSE

/*
    This file is part of starLiGHT.Input.

    starLiGHT.Input is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    starLiGHT.Input is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with starLiGHT.Input.  If not, see <http://www.gnu.org/licenses/>.

    
    ADDITIONAL (commercial) LICENSES for starLiGHT.Input are available on request.
*/

#endregion

#region Version Stuff
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 1652                  $:  Revision of last commit *
// * $Author:: glatzemann         $:  Author of last commit   *
// * $Date:: 2010-11-16 07:41:22 #$:  Date of last commit     *
// ************************************************************
#endregion

namespace starLiGHT.Input
{
#if WINDOWS_PHONE
    public class TouchpadDevice : IControlDevice
    {
        TouchPositionControl[] touches;
        int[] touchIds;

        public TouchpadDevice()
        {
            touches = new TouchPositionControl[4];
            touchIds = new int[4];

            for (int i = 0; i < touchIds.Length; i++)
            {
                touches[i] = new TouchPositionControl();
                touchIds[i] = -1;
            }
        }

        public bool IsConnected
        {
            get { return true; }
        }

        public DeviceType DeviceType
        {
            get { return DeviceType.TOUCHPAD; }
        }

        public bool HasPlayerIndex
        {
            get { return false; }
        }

        public PlayerIndex PlayerIndex
        {
            get { return PlayerIndex.One; }
        }

        public void Update(GameTime gameTime)
        {
            //
            // clear touches
            //
            for (int i = 0; i < touches.Length; i++)
            {
                touches[i].IsValid = false;
            }

            TouchCollection touchState = TouchPanel.GetState();

            foreach (var t in touchState)
            {
                bool cont = false;

                //
                // reuse touches
                //
                for (int i = 0; i < touchIds.Length; i++)
                {
                    if (touchIds[i] == t.Id)
                    {
                        touches[i].Update(t.Position);
                        cont = true;
                        break;
                    }
                }

                if (cont) continue;

                //
                // register new touches
                //
                for (int i = 0; i < touchIds.Length; i++)
                {
                    if (touchIds[i] == -1)
                    {
                        touches[i] = new TouchPositionControl(t.Position);
                        touchIds[i] = t.Id;
                    }
                }
            }

            //
            // free unused touch id's
            //
            for (int i = 0; i < touches.Length; i++)
            {
                if (!touches[i].IsValid)
                    touchIds[i] = -1;
            }
        }

        public IPositionalControl PositionControl(int index)
        {
            if (index > touches.Length)
                throw new ArgumentOutOfRangeException();

            return touches[index];
        }
    }
#endif
}
